<?php
/**
 * Created by PhpStorm.
 * 
 */

class ModIntensifyAttr extends Model {

    protected $table = 'intensify_attr';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    public function addmuch($params){
    	$result = $this->getDbConnection()->multiInsert($params, $this->table, FALSE);
    	return $result;
    }
    

    public function page($params){
    	$table = $this->table;
    	$where  = ' WHERE ia.game=:game ';
    	$args   = array(
    			':game'     => $params['g']
    	);
    	
        if(!empty($params['in_id'])){
            $where .= " AND ia.in_id=:in_id ";
            $args['in_id'] = $params['in_id'];
        }
        
        if(!empty($params['g_id'])){
        	$where .= " AND ia.g_id=:g_id ";
        	$args['g_id'] = $params['g_id'];
        }
        
        if(!empty($params['name'])){
        	$where .= " AND ia.name like :name ";
        	$args['name'] = '%'.$params['name'].'%';
        }
        
        if(!empty($params['g_name'])){
        	$where .= " AND g.name like :g_name ";
        	$args['g_name'] = '%'.$params['g_name'].'%';
        }
    	
    	$c_sql = "select count(1) as cnt from intensify_attr as ia INNER JOIN goods as g on g.g_id = ia.g_id {$where}";
    	$c_result = $this->getDbConnection()->query($c_sql, $args);
    	$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;

    	$order  = self::getOrder($params['order']);
    	$limit  = PubFunc::getLimit($params['length'], $params['page']);
    	//ia.id,ia.in_id,ia.g_id,ia.name,ia.name_type,ia.level
    	$sql = "SELECT ia.*,g.name as g_name,g.quality,g.stage,g.star from intensify_attr as ia INNER JOIN goods as g on g.g_id = ia.g_id {$where} {$order} {$limit}";
    	
    	$data = $this->getDbConnection()->query($sql, $args);
    	$result = array('count'=>$count,'data'=>$data);
    	return $result;
    }
    
    public static function getOrder($order){
    	if(PubFunc::checkEmpty($order, array(0, 1))) return '';
    	if(!in_array($order[1], array('asc', 'desc'))){
    		$sort = 'asc';
    	}else{
    		$sort = $order[1];
    	}
    	return " ORDER BY {$order[0]} {$sort} ";
    }
    
    public function getGid(){
    	$sql = "SELECT in_id FROM {$this->table}";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    public function getInfo($g_id_array,$level_array){
    	if(count($g_id_array) == 0 || count($level_array) == 0){
    		return array();
    	}
    	$g_ids = implode(',', $g_id_array);    
    	$levels = implode(',', $level_array);
    	$sql = "SELECT ia.*,g.name as g_name,g.quality,g.stage,g.star from intensify_attr as ia INNER JOIN goods as g on g.g_id = ia.g_id
    			WHERE ia.g_id in ({$g_ids}) and ia.level in ({$levels})";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    
    

    
    
} 